package com.weetop.aklyscan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.TimePickerView;
import com.blankj.utilcode.util.StringUtils;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.BaseList;
import com.weetop.aklyscan.bean.OutRecordListBean;
import com.weetop.aklyscan.presenter.RecordPresent;
import com.weetop.aklyscan.utils.BaseUtils;
import com.weetop.aklyscan.utils.MyTimePicker1;
import com.weetop.aklyscan.view.RecordView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OutRecordListActivity extends BaseActivity<RecordPresent> implements RecordView, CommonRecyclerAdapter.OnItemClickListener {

    /**
     * 生产商配件
     */
    public static final int ORDER_TYPE_MATTER_SC = 1001;
    /**
     * 生产商产品
     */
    public static final int ORDER_TYPE_PRODUCT_SC = 1002;

    /**
     * 品牌商线上
     */
    public static final int ORDER_TYPE_ONLINE_PP = 1003;

    /**
     * 品牌商线下
     */
    public static final int ORDER_TYPE_OFFLINE_PP = 1004;
    /**
     * 品牌商配件
     */
    public static final int ORDER_TYPE_MATTER_PP = 1005;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_huojia)
    TextView tv_huojia;
    @BindView(R.id.txt_title)
    TextView txt_title;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.tv_start_time)
    TextView startTimeView;
    @BindView(R.id.tv_end_time)
    TextView endTimeView;
    @BindView(R.id.tv_total_num)
    TextView totalNumView;
    @BindView(R.id.ll_start_time)
    View startTimeLayoutView;
    @BindView(R.id.ll_end_time)
    View endTimeLayoutView;
    private CommonRecyclerAdapter<OutRecordListBean> adapter;
    private List<OutRecordListBean> list = new ArrayList<>();
    private int order_type;
    private String order_sn;
    private String endTime;

    @Override
    protected RecordPresent createPresenter() {
        return new RecordPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_out_record_list);
        ButterKnife.bind(this);
        order_sn = getIntent().getStringExtra("order_sn");
        order_type = getIntent().getIntExtra("order_type", ORDER_TYPE_MATTER_PP);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<OutRecordListBean>(mActivity, R.layout.item_record_list, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, OutRecordListBean item, int position) {
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                if (order_type == ORDER_TYPE_PRODUCT_SC) {
                    helper.setText(R.id.tv_time, item.getUpdate_time());
                    helper.setText(R.id.tv_num, item.getTotal_num());
                } else {
                    helper.setText(R.id.tv_time, item.getCk_time());
                    helper.setText(R.id.tv_num, TextUtils.isEmpty(item.getCount_num()) ? item.getCount_ck_num() : item.getCount_num());
                }
                helper.setText(R.id.tv_huojia, item.getApply_user_name());
            }
        };
        tv_huojia.setText("操作人员");
        txt_title.setText("出库记录");
        tv_time.setText("出库时间");
        tv_num.setText("出库数量");
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        mPresenter.Into77();

        if (order_type == ORDER_TYPE_ONLINE_PP) {
            //品牌端端 线上产品出库
            startTimeLayoutView.setVisibility(View.GONE);
            endTimeLayoutView.setVisibility(View.GONE);
        } else if (order_type == ORDER_TYPE_OFFLINE_PP) {
            //品牌端端 线下产品出库
            endTimeLayoutView.setVisibility(View.GONE);
            startTimeLayoutView.setVisibility(View.GONE);
        } else if (order_type == ORDER_TYPE_MATTER_PP) {
            //品牌端端 配件出库
            endTimeLayoutView.setVisibility(View.GONE);
            startTimeLayoutView.setVisibility(View.GONE);
        } else if (order_type == ORDER_TYPE_MATTER_SC) {
            //生产端 配件出库
            startTimeLayoutView.setVisibility(View.GONE);
            endTimeLayoutView.setVisibility(View.GONE);
        } else {
            //生产端 产品出库
            startTimeLayoutView.setVisibility(View.GONE);
            endTimeLayoutView.setVisibility(View.GONE);
        }
    }

    @Override
    public void getInto77Success(BaseModel<String> o) {
        endTime = o.getData();
        query();
    }

    @OnClick({R.id.iv_back, R.id.ll_start_time, R.id.ll_end_time, R.id.query})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (R.id.iv_back == id) {
            finish();
        } else if (R.id.ll_start_time == id) {
            MyTimePicker1 myTimePicker2 = new MyTimePicker1();
            Calendar endCalendar;
            if (StringUtils.isEmpty(endTime)) {
                endCalendar = BaseUtils.dateToCalendar();
            } else {
                endCalendar = BaseUtils.StringTocalendar(endTime);
            }

            TimePickerView pickerView2 = myTimePicker2.selectTime(this,
                    BaseUtils.StringTocalendar(startTimeView.getText().toString().trim()), endCalendar);
            myTimePicker2.setOnSelectListener(new MyTimePicker1.OnSelectListener() {
                @Override
                public void onSelect(Date date, View v) {
                    startTimeView.setText(BaseUtils.dateToStrLong(date, "yyyy-MM-dd"));
                }
            });
            pickerView2.show();
        } else if (R.id.ll_end_time == id) {
            MyTimePicker1 myTimePicker2 = new MyTimePicker1();
            Calendar endCalendar;
            if (StringUtils.isEmpty(endTime)) {
                endCalendar = BaseUtils.dateToCalendar();
            } else {
                endCalendar = BaseUtils.StringTocalendar(endTime);
            }
            TimePickerView pickerView2 = myTimePicker2.selectTime(this,
                    BaseUtils.StringTocalendar(endTimeView.getText().toString().trim()), endCalendar);
            myTimePicker2.setOnSelectListener(new MyTimePicker1.OnSelectListener() {
                @Override
                public void onSelect(Date date, View v) {
                    endTimeView.setText(BaseUtils.dateToStrLong(date, "yyyy-MM-dd"));
                }
            });
            pickerView2.show();
        } else if (R.id.query == id) {
            query();
        }
    }

    private void query() {
//        String startTime = startTimeView.getText().toString();
//        String endTime = endTimeView.getText().toString();
//        if (TextUtils.isEmpty(startTime)) {
//            ToastUtils.showShort("必须输入开始时间!");
//            return;
//        }
//        if (TextUtils.isEmpty(endTime)) {
//            ToastUtils.showShort("必须输入结束时间!");
//            return;
//        }
        String startTime = "";
        String endTime = "";
        if (order_type == ORDER_TYPE_ONLINE_PP) {
            mPresenter.Out13(order_sn, "", "");
        } else if (order_type == ORDER_TYPE_OFFLINE_PP) {
            mPresenter.Out11(order_sn, "", "");
        } else if (order_type == ORDER_TYPE_MATTER_PP) {
            mPresenter.Out15(order_sn, "", "");
        } else if (order_type == ORDER_TYPE_MATTER_SC) {
            mPresenter.Into29(order_sn, "", "");
        } else {
            mPresenter.newInto29(order_sn);
        }
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {

        Intent intent = new Intent();
        if (order_type == ORDER_TYPE_ONLINE_PP || order_type == ORDER_TYPE_OFFLINE_PP
                || order_type == ORDER_TYPE_MATTER_PP || order_type == ORDER_TYPE_PRODUCT_SC) {
            intent.setClass(OutRecordListActivity.this, OutPeiJianRecordActivity.class);
        } else {
            intent.setClass(OutRecordListActivity.this, OutPeiJianRecordActivity1.class);
        }
        list.get(i).setOrder_sn(order_sn);
        intent.putExtra("record_model", list.get(i));
        intent.putExtra("order_type", order_type);
        startActivity(intent);
    }

    @Override
    public void getOut09Success(BaseModel<BaseList<OutRecordListBean>> o) {
        List<OutRecordListBean> listBeans = o.getData().getList_data();
        list.clear();
        adapter.clear();
        list.addAll(listBeans);
        adapter.addAll(listBeans);
        int total = 0;

        for (OutRecordListBean bean :
                o.getData().getList_data()) {
            if (order_type == ORDER_TYPE_PRODUCT_SC) {
                total += Integer.parseInt(bean.getTotal_num());
            } else {
                total += Integer.parseInt(TextUtils.isEmpty(bean.getCount_num()) ? bean.getCount_ck_num() : bean.getCount_num());
            }
        }
        totalNumView.setText(total + "");
    }

    @Override
    public void getInto29Success(BaseModel<List<OutRecordListBean>> o) {
        List<OutRecordListBean> listBeans = o.getData();
        list.clear();
        adapter.clear();
        list.addAll(listBeans);
        adapter.addAll(listBeans);
        int total = 0;

        for (OutRecordListBean bean :
                o.getData()) {
            if (order_type == ORDER_TYPE_PRODUCT_SC) {
                total += Integer.parseInt(bean.getTotal_num());
            } else {
                total += Integer.parseInt(TextUtils.isEmpty(bean.getCount_num()) ? bean.getCount_ck_num() : bean.getCount_num());
            }
        }
        totalNumView.setText(total + "");
    }
}
